<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../static/layui/css/layui.css" media="all">
    <script src="../../static/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../../static/javascript/jquery.min.js"></script>

</head>
<script>
    $(document).ready(function () {

    });
</script>

<body>
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
        <legend>录入成绩</legend>
    </fieldset>
    <form class="layui-form layui-form-pane" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">学号</label>
            <div class="layui-input-inline">
                <input type="text" name="sno" id="sno" autocomplete="off" lay-verify="sno" placeholder="请输入学生学号"
                       class="layui-input">
            </div>
        </div>
 <!--       <div class="layui-form-item">
            <label class="layui-form-label">学生名字</label>
            <div class="layui-input-inline">
                <input type="text" name="name" id="name" autocomplete="off" lay-verify="name" placeholder="请输入学生名字"
                    class="layui-input">
            </div>
        </div>-->
        <div class="layui-form-item">
            <label class="layui-form-label">课程</label>
            <div class="layui-input-inline">
                <input type="text" name="course" id="course" autocomplete="off" lay-verify="course"  placeholder="请输入课程"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">分数</label>
            <div class="layui-input-inline">
                <input type="text" name="score" id="score" placeholder="请输入分数" lay-verify="score" autocomplete="off"
                       class="layui-input">
            </div>
        </div>

        <div class="layui-form-item layui-form-mid">
            <div class="layui-form-item">

            </div>
            <button class="layui-btn" lay-submit="" style="position:absolute;left:150px;"
                lay-filter="demo">确定</button>
        </div>
        <input hidden id="id" th:value="${id}">
    </form>

    <script>
        //Integer id, String name, String phone ,Integer countLimit
    //var myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(17[0]{1})|(15[0-3]{1})|(15[5-9]{1})|(18[0-9]{1}))+\d{8})$/;
    layui.use(['form', 'layedit', 'laydate'], function () {
            var id = $("#id").val()
            if (id>0) {
                var u = "/score/find?id=" + id;
                $.get(u, function (data, status) {
                    console.log(u)
                    var ret = JSON.parse(JSON.stringify(data));
                    if (ret.status == 1) {
                        alert(ret.msg);
                    } else {
                        $("#name").val(ret.data.stu_name)
                        $("#sno").val(ret.data.sno)
                        $("#score").val(ret.data.score)
                        $("#countLimit").val(ret.data.countLimit)
                        $("#course").val(ret.data.course_name)

                    }
                });
            }
            var form = layui.form
                , layer = layui.layer
                , layedit = layui.layedit
                , laydate = layui.laydate;

            //创建一个编辑器
            var editIndex = layedit.build('LAY_demo_editor');

            //自定义验证规则
            var patrn = /^\+?[1-9][0-9]*$/;
            var myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(17[0]{1})|(15[0-3]{1})|(15[5-9]{1})|(18[0-9]{1}))+\d{8})$/;

            form.verify({
                sno: function (value) {
                    if (value = null || value.length == 0) {
                        return '学生学号不能为空';
                    }
                }/*,
                name: function (value) {
                    if (value = null || value.length == 0) {
                        return '学生姓名不能为空';
                    }
                }*/
                , course: function (value) {
                    if (value = null || value.length == 0) {
                        return '课程不能为空';
                    }
                }
                , score: function (value) {
                    if (value == null || value > 100 || value < 0) {
                        return '请输入正确分数';
                    }
                }
                , content: function (value) {
                    layedit.sync(editIndex);
                }
            });
            //监听提交
            form.on('submit(demo)', function (data) {
        //Integer id, String name, String phone ,Integer countLimit

                var url;
                if (id==0) {
                    url = "/score/add?sno="+$("#sno").val()+ "&score=" + $("#score").val()+ "&course=" + $("#course").val()
                } else if (id>0) {
                    url = "/score/update?id=" + id +  "&score=" + $("#score").val()+ "&course=" + $("#course").val()
                }
                console.log(url)
                var datas = JSON.parse(JSON.stringify(data.field));
                $.get(url, function (data, status) {
                    var ret = JSON.parse(JSON.stringify(data));
                    if (ret.status == 1) {
                        layer.alert(ret.msg);
                    } else {
                        layer.alert(ret.msg);
                        window.parent.location.reload();//刷新父页面
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭当前iframe页面
                        parent.location.reload();//刷新父页面
                    }

                });
                return false;
            });
        });
    </script>

</body>

</html>
